#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "FileDialogs.h"
#include "DumpPEInfo.h"

int main(int argc, char** argv)
{
	TCHAR* filePath = NULL;
	BOOL freeFilePath = FALSE;
	BOOL selectedFileWithDialog = FALSE;
	if (argc == 1)
	{
		static const TCHAR  catagory [] = TEXT("Executable File");
		static const TCHAR* exts [] = {TEXT("exe"), TEXT("dll"), TEXT("obj"), NULL};

SelectFileWithDialog:
		if (!(filePath = OpenFileDlg(catagory, exts, NULL)))
			return -1;
		selectedFileWithDialog = TRUE;
	}
	else
	{
		int i;
		for (i = 1; i <= argc; ++i)
		{
			if (GetFileAttributesA(argv[i]) != INVALID_FILE_ATTRIBUTES)
			{
#ifndef UNICODE
				filePath = argv[i];
#else
				unsigned int argvLen, j;

				freeFilePath = TRUE;
				argvLen = strlen(argv[i]);
				filePath = (TCHAR*)malloc(sizeof(TCHAR)*argvLen + 2);
				for (j = 0; j < argvLen; ++j)
					filePath[j] = (TCHAR)argv[i][j];
				filePath[j] = (TCHAR)'\0';
#endif
			}
		}

		if (filePath == NULL)
			goto SelectFileWithDialog;
	}

	DumpPEInfo(filePath);

	if (freeFilePath)
		free(filePath);

#define MAKE_STR(LIT) #LIT
#define PRINTF_SIZE_OF_TYPE(TYPE)\
	printf("sizeof(" MAKE_STR(TYPE) ") = %u\n", sizeof(TYPE));

	PRINTF_SIZE_OF_TYPE(unsigned char);
	PRINTF_SIZE_OF_TYPE(unsigned short);
	PRINTF_SIZE_OF_TYPE(unsigned int);
	PRINTF_SIZE_OF_TYPE(unsigned long);
	PRINTF_SIZE_OF_TYPE(unsigned long long);
	if (selectedFileWithDialog)
		system("pause");
	return 0;
}